package com.alvin.abp.common.context;


import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alvin.abp.domain.AlvinUser;

public class CurrentUserContext {

	public static String CURRENT_USER = "principal";
	
	public static String AUTHED_USER = "authed";
	
	public static AlvinUser getCurrentUser() {
		AlvinUser user = (AlvinUser) session().getAttribute(CurrentUserContext.CURRENT_USER);
		return user;
	}
	
	public static void setCurrentUser(AlvinUser user) {
		session().setAttribute(CurrentUserContext.CURRENT_USER, user);
	}
	
	public static boolean isAnonymous() {
		if(session().getAttribute(CurrentUserContext.CURRENT_USER) == null) {
			return true;
		}
		return false;
	}
	
	public static void invalidateUser(){
		session().invalidate();
	}
	
	public static HttpSession session() {
	    ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
	    return attr.getRequest().getSession(false); // true == allow create
	}

}
